using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using WcfContract;

namespace TestServer
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("New Service Host");
            Console.WriteLine("CLR Version: " + System.Environment.Version.ToString());

            Console.WriteLine("Create Buffered Pipe Service Host");
            ServiceHost serviceBufferedPipesHost = null;
            try
            {
                NetNamedPipeBinding bufferedPipesBinding = new NetNamedPipeBinding();
                bufferedPipesBinding.TransferMode = TransferMode.Buffered;
                bufferedPipesBinding.Security.Mode = NetNamedPipeSecurityMode.None;
                serviceBufferedPipesHost = new ServiceHost(typeof(ServiceBuffered));
                serviceBufferedPipesHost.AddServiceEndpoint(typeof(IServiceBufferedContract), bufferedPipesBinding, new Uri("net.pipe://localhost/TestServer/ServiceBuffered"));

                Console.WriteLine("Open Buffered Pipe Service Host");
                serviceBufferedPipesHost.Open();

                Console.WriteLine("Buffered Pipe Service Host is running");
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to Create Buffered Pipe Service Host");
                Console.WriteLine(e.ToString());
                Console.WriteLine();
            }


            Console.WriteLine("Create Buffered TCP Service Host");
            ServiceHost serviceBufferedTcpHost = null;
            try
            {
                NetTcpBinding bufferedTcpBinding = new NetTcpBinding();
                bufferedTcpBinding.TransferMode = TransferMode.Buffered;
                bufferedTcpBinding.Security.Mode = SecurityMode.None; 
                serviceBufferedTcpHost = new ServiceHost(typeof(ServiceBuffered));
                serviceBufferedTcpHost.AddServiceEndpoint(typeof(IServiceBufferedContract), bufferedTcpBinding, new Uri("net.tcp://localhost/TestServer/ServiceBuffered"));

                Console.WriteLine("Open Buffered TCP Service Host");
                serviceBufferedTcpHost.Open();

                Console.WriteLine("Buffered TCP Service Host is running");
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to Create Buffered TCP Service Host");
                Console.WriteLine(e.ToString());
                Console.WriteLine();
            }

            /*
            NetNamedPipeBinding streamedBinding = new NetNamedPipeBinding();
            streamedBinding.TransferMode = TransferMode.Streamed;
            Console.WriteLine("New Streamed Service Host"); 
            ServiceHost serviceStreamedHost = new ServiceHost(typeof(ServiceStreamed));
            serviceStreamedHost.AddServiceEndpoint(typeof(IServiceStreamedContract), streamedBinding, new Uri("net.pipe://localhost/TestServer/ServiceStreamed"));
            Console.WriteLine("Open Streamed Service Host"); 
            serviceStreamedHost.Open();
            */

            Console.WriteLine("Server Running, press 's' to stop...");
            while (Console.ReadKey().KeyChar != 's') { }

            Console.WriteLine("Stopping Server");

            if( serviceBufferedPipesHost != null )
                serviceBufferedPipesHost.Close();

            if (serviceBufferedTcpHost != null)
                serviceBufferedTcpHost.Close();
            
            Console.WriteLine("Server is Shutdown");

        }
    }
}
